<?php

namespace common\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Project;

/**
 * ProjectSearch represents the model behind the search form about `common\models\Project`.
 */
class ProjectSearch extends Project
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'package_id', 'category', 'status', 'province', 'city', 'management_status', 'currency', 'is_highlights', 'guarantor_status', 'mortgage_type', 'lock_user'], 'integer'],
            [['code', 'title', 'access', 'access_users', 'borrower', 'industry', 'principal', 'interest', 'highlights', 'guarantor', 'warranty', 'created_at', 'lock_timestamp'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Project::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'package_id' => $this->package_id,
            'category' => $this->category,
            'status' => $this->status,
            'province' => $this->province,
            'city' => $this->city,
            'management_status' => $this->management_status,
            'currency' => $this->currency,
            'is_highlights' => $this->is_highlights,
            'guarantor_status' => $this->guarantor_status,
            'mortgage_type' => $this->mortgage_type,
            'created_at' => $this->created_at,
            'lock_user' => $this->lock_user,
        ]);

        $query->andFilterWhere(['like', 'code', $this->code])
            ->andFilterWhere(['like', 'title', $this->title])
            ->andFilterWhere(['like', 'access', $this->access])
            ->andFilterWhere(['like', 'access_users', $this->access_users])
            ->andFilterWhere(['like', 'borrower', $this->borrower])
            ->andFilterWhere(['like', 'industry', $this->industry])
            ->andFilterWhere(['like', 'principal', $this->principal])
            ->andFilterWhere(['like', 'interest', $this->interest])
            ->andFilterWhere(['like', 'highlights', $this->highlights])
            ->andFilterWhere(['like', 'guarantor', $this->guarantor])
            ->andFilterWhere(['like', 'warranty', $this->warranty])
            ->andFilterWhere(['like', 'lock_timestamp', $this->lock_timestamp]);

        return $dataProvider;
    }
}
